using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using database;
using objetos;

namespace dados
{
	#region TB_NF_OBS_CONTRIBUINTE
	/// <summary>
	/// This object represents the properties and methods of a TB_NF_OBS_CONTRIBUINTE.
	/// </summary>
	public class DAL_TB_NF_OBS_CONTRIBUINTE
	{		
	
		/// <summary>
		/// Incluir via aplicacao
		/// </summary>	
		public int Incluir(OBJ_TB_NF_OBS_CONTRIBUINTE obj)
		{
			try
			{
				DAO_DATABASE db = new DAO_DATABASE();
				DataSet ds = new DataSet();
				List<string> lista = new List<string>();		
	
				db.CreateParameter("@id_nf", SqlDbType.Int, obj.id_nf);				
				lista.Add("@id_nf");				
				db.CreateParameter("@xCampo", SqlDbType.VarChar, obj.xCampo);				
				lista.Add("@xCampo");				
				db.CreateParameter("@xTexto", SqlDbType.VarChar, obj.xTexto);				
				lista.Add("@xTexto");				
	
				string query = String.Format("Insert Into tb_nf_obs_contribuinte ({0}) Values ({1}); SELECT SCOPE_IDENTITY()",  string.Join(",",lista.ToArray()).ToString().Replace("@", ""), string.Join(",",lista.ToArray()).ToString());
				ds = db.ExecuteQuery(query);
				
				return Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
			}
			catch (Exception)
       		{
            	throw;
        	}
		}
		
		/// <summary>
		/// Update via aplicacao
		/// </summary>			
		public void Alterar(OBJ_TB_NF_OBS_CONTRIBUINTE obj)
		{
			try
			{
				DAO_DATABASE db = new DAO_DATABASE();
				List<string> lista = new List<string>();
	
				db.CreateParameter("@id", SqlDbType.Int, obj.id);
				
	
				db.CreateParameter("@id_nf", SqlDbType.Int, obj.id_nf);
				lista.Add("id_nf = @id_nf");
				db.CreateParameter("@xCampo", SqlDbType.VarChar, obj.xCampo);
				lista.Add("xCampo = @xCampo");
				db.CreateParameter("@xTexto", SqlDbType.VarChar, obj.xTexto);
				lista.Add("xTexto = @xTexto");
	
				string query = String.Format("Update TB_NF_OBS_CONTRIBUINTE Set {0} Where id = @id", string.Join(",",lista.ToArray()).ToString());
				db.ExecuteQuery(query);
			}
			catch (Exception)
       		{
            	throw;
        	}
		}	
		
		/// <summary>
		/// Delete viaaplicacao
		/// </summary>	
		public void Excluir(OBJ_TB_NF_OBS_CONTRIBUINTE obj)
		{
			try
			{
				DAO_DATABASE db = new DAO_DATABASE();
				StringBuilder queryParameters = new StringBuilder();
				db.CreateParameter("@id", SqlDbType.Int, obj.id);
		
				string query = "Delete tb_nf_obs_contribuinte Where id = @id";			
				db.ExecuteQuery(query);
			}
			catch (Exception)
       		{
            	throw;
        	}
		}
	
	
		/// <summary>
		/// Lista objeto
		/// </summary>						
		public List<OBJ_TB_NF_OBS_CONTRIBUINTE> SelecionarLista()
    	{
        	try
        	{
				DAO_DATABASE db = new DAO_DATABASE();
				List<OBJ_TB_NF_OBS_CONTRIBUINTE> lista = new List<OBJ_TB_NF_OBS_CONTRIBUINTE>();
				OBJ_TB_NF_OBS_CONTRIBUINTE obj = null;
				DataTable dt = new DataTable();
				DataSet ds = new DataSet();
				StringBuilder sb = new StringBuilder();
				sb.Append("Select * from  TB_NF_OBS_CONTRIBUINTE ");
        
	            ds = db.ExecuteQuery(sb.ToString());
				
				if(!db.DataSetIsEmpty(ds))
				{
					dt = ds.Tables[0];
					
					foreach (DataRow dr in dt.Rows)
					{											
						obj = new OBJ_TB_NF_OBS_CONTRIBUINTE();
						Set(obj,dr);
						lista.Add(obj);
		
					}
				}
				return lista;
        	}
        	catch (Exception)
       		{
            	throw;
        	}

    	}
		
		/// <summary>
		/// Set Objeto
		/// </summary>	
		private void Set(OBJ_TB_NF_OBS_CONTRIBUINTE obj,DataRow dr)
		{
			obj.id = Convert.ToInt32(dr["id"]);						
			obj.id_nf = Convert.ToInt32(dr["id_nf"].ToString()) ;	
			obj.xCampo = dr["xCampo"] == DBNull.Value ?  string.Empty : dr["xCampo"].ToString() ;
			obj.xTexto = dr["xTexto"] == DBNull.Value ?  string.Empty : dr["xTexto"].ToString() ;
			
		}						
				
		/// <summary>
		/// Selecionar Objeto Por ID
		/// </summary>	
		public OBJ_TB_NF_OBS_CONTRIBUINTE SelecionarObjetoPorId(int id)
		{
			try
			{
				DAO_DATABASE db = new DAO_DATABASE();
				OBJ_TB_NF_OBS_CONTRIBUINTE obj = new OBJ_TB_NF_OBS_CONTRIBUINTE();
				DataSet ds = new DataSet();				
				DataTable dt = new DataTable();
				
				db.CreateParameter("@id", SqlDbType.Int, id);
				StringBuilder sb = new StringBuilder();
				sb.Append("SELECT * FROM tb_nf_obs_contribuinte  Where id = @id");
				ds = db.ExecuteQuery(sb.ToString());				
				
				if(!db.DataSetIsEmpty(ds))
				{
					dt = ds.Tables[0];
					
					foreach (DataRow dr in dt.Rows)
                	{
						obj = new OBJ_TB_NF_OBS_CONTRIBUINTE();
						Set(obj,dr);																			
					}
					return obj;
					
				}
				else
				{
					return null;	
				}			
			}
			catch (Exception)
       		{
            	throw;
        	}
			
		}
					
		
		
		/// <summary>
		/// Lista objeto Parametro Where OrderBy
		/// </summary>							
		public List<OBJ_TB_NF_OBS_CONTRIBUINTE> SelecionarLista(String Where, String OrderBy)
    	{
        	try
        	{
				DAO_DATABASE db = new DAO_DATABASE();
				OBJ_TB_NF_OBS_CONTRIBUINTE obj = null;
				DataSet ds = new DataSet();
				DataTable dt = new DataTable();
				List<OBJ_TB_NF_OBS_CONTRIBUINTE> lista = new List<OBJ_TB_NF_OBS_CONTRIBUINTE>();
				
				StringBuilder sb = new StringBuilder();
				db.CreateParameter("@Where",SqlDbType.VarChar, Where );
				db.CreateParameter("@OrderBy", SqlDbType.VarChar, OrderBy );				
				
				sb.Append("SELECT * ");
				sb.Append(" FROM TB_NF_OBS_CONTRIBUINTE");
				if(Where != string.Empty)				
				sb.Append(string.Format(" Where {0} ",Where));
				if(OrderBy != string.Empty)
				sb.Append(string.Format(" Order by {0}",OrderBy));
				
				ds = db.ExecuteQuery(sb.ToString());
				
				if(!db.DataSetIsEmpty(ds))
				{
					dt = ds.Tables[0];
					foreach (DataRow dr in dt.Rows)
					{						
						obj = new OBJ_TB_NF_OBS_CONTRIBUINTE();
						Set(obj,dr);			
						lista.Add(obj);
					}
				}
				return lista;
        	}
        	catch (Exception)
       		{
            	throw;
        	}

    	}

		
		public DataSet Selecionar(String Where, String OrderBy, int PageIndex, int PageSize )
		{
				DAO_DATABASE db = new DAO_DATABASE();
				DataSet ds = new DataSet();
				
				StringBuilder sb = new StringBuilder();
				
				PageIndex = PageSize * PageIndex;
            	PageSize =  PageIndex + PageSize;
				
		      	if (OrderBy == string.Empty) OrderBy = "tb_nf_obs_contribuinte.id";

            	sb.Append("SELECT * FROM ");
            	sb.Append(string.Format(" (SELECT ROW_NUMBER() OVER (ORDER BY {0}) AS RowNumber,",OrderBy));
           		sb.Append(" * FROM   tb_nf_obs_contribuinte ");
				if (Where != string.Empty)
				{
					sb.Append(string.Format(" WHERE {0}", Where));
				}
				sb.Append(" ) query  ");
				sb.Append(string.Format(" WHERE RowNumber BETWEEN {0} + 1 and {1}; ",PageIndex,PageSize));

            	sb.Append("SELECT count(*) FROM tb_nf_obs_contribuinte");
				if (Where != string.Empty)
				{
					sb.Append(string.Format(" WHERE {0}", Where));
				}
				ds = db.ExecuteQuery(sb.ToString());
				
				if (!db.DataSetIsEmpty(ds))
            	{
                publico.PUB_PAGINACAO.CalcularTotalDePaginas(Convert.ToDecimal(ds.Tables[1].Rows[0][0].ToString()));
            	}

				
            	return ds;
								
		}
				
								
		public DataSet Selecionar(String Where, String OrderBy)
        {
            DAO_DATABASE db = new DAO_DATABASE();
            DataSet ds = new DataSet();
            StringBuilder sb = new StringBuilder();

            sb.Append("Select * from  TB_NF_OBS_CONTRIBUINTE ");
           if (Where != string.Empty)
            {
                db.CreateParameter("@Where", SqlDbType.VarChar, Where);
                sb.Append(string.Format(" Where  {0}",Where));
            }

            if (OrderBy != string.Empty)
            {
                db.CreateParameter("@OrderBy", SqlDbType.VarChar, OrderBy);
                sb.Append(string.Format(" Order By {0}", OrderBy));
            }

            ds = db.ExecuteQuery(sb.ToString());
            return ds;
        }		
		
		public DataSet Selecionar()
        {
            DAO_DATABASE db = new DAO_DATABASE();
            DataSet ds = new DataSet();
            StringBuilder sb = new StringBuilder();

            sb.Append("Select * from  TB_NF_OBS_CONTRIBUINTE Order By id");            
            ds = db.ExecuteQuery(sb.ToString());
            return ds;
        }		
		
		
		public DataSet ListaCampos()
        {

            DAO_DATABASE db = new DAO_DATABASE();
            DataSet ds = new DataSet();
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT TABLE_NAME, ORDINAL_POSITION, ");
            sb.Append(" COLUMN_NAME, DATA_TYPE, IS_NULLABLE ");
            sb.Append(" FROM INFORMATION_SCHEMA.COLUMNS");
            sb.Append(" WHERE TABLE_NAME = 'tb_nf_obs_contribuinte'");
            ds = db.ExecuteQuery(sb.ToString());

            return ds;

        }

		public DataSet DropdownList()
        {
            DAO_DATABASE db = new DAO_DATABASE();
            DataSet ds = new DataSet();
            StringBuilder sb = new StringBuilder();
			sb.Append("SELECT ");
			sb.Append(" id ");
				sb.Append(", id_nf ");  				
				sb.Append(", xCampo ");  				
				sb.Append(", xTexto ");  				
			sb.Append(" FROM tb_nf_obs_contribuinte ");           
			sb.Append(" ORDER BY id_nf ");
            ds = db.ExecuteQuery(sb.ToString());

            // ds = db.ExecuteProcedure("USP_TB_NF_OBS_CONTRIBUINTE_DROPDOWNLIST");  
            return ds;
        }

        public DataSet DropdownList(String Where)
        {
            DAO_DATABASE db = new DAO_DATABASE();
            DataSet ds = new DataSet();
            StringBuilder sb = new StringBuilder();
            db.CreateParameter("@Where", SqlDbType.VarChar, Where);
			sb.Append("SELECT ");
			sb.Append(" id ");
			
			
			sb.Append(", id_nf ");  				
			
			
			sb.Append(", xCampo ");  				
			
			
			sb.Append(", xTexto ");  				
			sb.Append(" FROM tb_nf_obs_contribuinte ");
			sb.Append(string.Format(" WHERE {0} ",Where));
			sb.Append(" ORDER BY id_nf ");
            ds = db.ExecuteQuery(sb.ToString());

            // ds = db.ExecuteProcedure("USP_TB_NF_OBS_CONTRIBUINTE_DROPDOWNLIST");
            return ds;
        }
		
		  public DataSet DropdownList(String Where, String OrderBy)
        {
            DAO_DATABASE db = new DAO_DATABASE();
            DataSet ds = new DataSet();
            StringBuilder sb = new StringBuilder();
            db.CreateParameter("@Where", SqlDbType.VarChar, Where);
			sb.Append("SELECT ");
			sb.Append(" id ");
			sb.Append(", id_nf ");  				
			sb.Append(", xCampo ");  				
			sb.Append(", xTexto ");  				
			sb.Append(" FROM tb_nf_obs_contribuinte ");
			sb.Append(string.Format(" WHERE {0} ORDER BY {1}",Where,OrderBy));			
            ds = db.ExecuteQuery(sb.ToString());

            // ds = db.ExecuteProcedure("USP_TB_NF_OBS_CONTRIBUINTE_DROPDOWNLIST");
            return ds;
        }

	}
	#endregion
}

